package com.shsubway.activity.com;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.shsubway.R;
import com.shsubway.activity.manger.HomeActivity;
import com.shsubway.base.BaseActivity;
import com.shsubway.model.StaffEntity;
import com.shsubway.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.smssdk.SMSSDK;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.user_name)
    MaterialEditText userName;
    @BindView(R.id.user_pwd)
    MaterialEditText userPwd;
    @BindView(R.id.login_btn)
    Button loginBtn;
    @BindView(R.id.forget_btn)
    TextView forgetBtn;
    @BindView(R.id.regint_btn)
    Button regintBtn;
    AndroidNextInputs androidNextInputs;
    Intent intent;
    Bundle bundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();

        androidNextInputs = new AndroidNextInputs();
        androidNextInputs.add(userName, StaticScheme.NotBlank().msgOnFail("账号不能为空"), StaticScheme.ChineseMobile().msgOnFail("请输入正确的手机号码"));
        androidNextInputs.add(userPwd, StaticScheme.NotBlank().msgOnFail("密码不能为空"));
    }

    @OnClick({R.id.forget_btn, R.id.regint_btn, R.id.login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.forget_btn:
                intent.putExtra(Constant.TYPE,1);
                intent.setClass(context,RgisterActivity.class);
                startActivity(intent);
                break;
            case R.id.regint_btn:
                intent.putExtra(Constant.TYPE,0);
                intent.setClass(context,RgisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login_btn:
                boolean passeds = androidNextInputs.test();
                if (passeds) {
                    checkUserAndPwd();
                }
                break;
        }
    }

    private void saveMsg(StaffEntity staffEntity) {
        put(Constant.ISLOGIN,true);
        put(Constant.STAFF_PHONE,staffEntity.getStaff_phone());
        put(Constant.STAFF_PWD,staffEntity.getStaff_pwd());
        put(Constant.USER_ID,staffEntity.getObjectId());
        startActivity(HomeActivity.class);
        finish();
    }

    private void checkUserAndPwd() {
        BmobQuery<StaffEntity> eq1 = new BmobQuery<StaffEntity>();
        eq1.addWhereEqualTo("staff_phone", userName.getText().toString().trim());
        //--and条件2
        BmobQuery<StaffEntity> eq2 = new BmobQuery<StaffEntity>();
        eq2.addWhereEqualTo("staff_pwd", userPwd.getText().toString().trim());
        List<BmobQuery<StaffEntity>> queries = new ArrayList<BmobQuery<StaffEntity>>();
        queries.add(eq1);
        queries.add(eq2);

        //查询符合整个and条件的人
        BmobQuery<StaffEntity> query = new BmobQuery<StaffEntity>();
        query.and(queries);
        query.findObjects(new FindListener<StaffEntity>() {
            @Override
            public void done(List<StaffEntity> list, BmobException e) {
                if(e==null){
                    if (list.size()==1){
                        saveMsg(list.get(0));
                    }else{
                        toast("账号或密码错误。");
                    }
                }else{
                    toast("查询失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
    }

}
